// -----------------------------------------------------------------------
// <copyright file="FileSystemProvider.cs" company="Softserve">
// File System
// </copyright>
// -----------------------------------------------------------------------

using WebServer.BusinessLogic.Helpers;

namespace WebServer.BusinessLogic.Handlers.Interface
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using WebServer.BusinessLogic.Web.Cache;

    /// <summary>
    /// Class which helps mocking
    /// </summary>
    public class FileSystemProvider : IFileSystemProvider
    {
        /// <summary>
        /// Check existing directory info
        /// </summary>
        /// <param name="path">path to the directory</param>
        /// <returns>bool existing</returns>
        public bool DirectoryExists(string path)
        {
            return Directory.Exists(path);
        }

        /// <summary>
        /// Creating directory info
        /// </summary>
        /// <param name="path">path to the directory</param>
        /// <returns>DirectoryInfo object</returns>
        public DirectoryInfo CreateDirectoryInfo(string path)
        {
            return new DirectoryInfo(path);
        }

        /// <summary>
        /// Getting file system infos
        /// </summary>
        /// <param name="path">path to the file or directory</param>
        /// <returns>file system info</returns>
        public IList<FileSystemInfoModel> GetFileSystemInfos(string path)
        {
            var di = this.CreateDirectoryInfo(path);
            var fsi = di.GetFileSystemInfos();

            var myFsiList = fsi.Select(fs => new FileSystemInfoModel(fs.Name, fs.Extension, fs.FullName)).ToList();

            return myFsiList;
        }

        /// <summary>
        /// Check file exists
        /// </summary>
        /// <param name="path">path to the file</param>
        /// <returns>bool file exist</returns>
        public bool FileExists(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// Getting data or file
        /// </summary>
        /// <param name="cacheKey">key of cache</param>
        /// <param name="filePath">path to the file</param>
        /// <returns>data array</returns>
        public byte[] GetDataOrFile(string cacheKey, string filePath)
        {
            return CacheManager.Instance.GetCacheOrFile(cacheKey, filePath);
        }

        /// <summary>
        /// Reads data from file
        /// </summary>
        /// <param name="pathToFile">Filepath</param>
        /// <returns>Bytes of file</returns>
        public byte[] ReadBytesFromFile(string pathToFile)
        {
            return Utils.ReadBytesFromFile(pathToFile);
        }
    }

    /// <summary>
    /// Class for File System Info Model
    /// </summary>
    public class FileSystemInfoModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileSystemInfoModel" /> class
        /// </summary>
        /// <param name="name">name of file</param>
        /// <param name="extension">file extension</param>
        /// <param name="fullName">full name of file</param>
        public FileSystemInfoModel(string name, string extension, string fullName)
        {
            this.Name = name;
            this.Extension = extension;
            this.FullName = fullName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileSystemInfoModel" /> class
        /// </summary>
        public FileSystemInfoModel()
        {
        }

        /// <summary>
        /// Gets or sets name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets extension
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// Gets or sets full name
        /// </summary>
        public string FullName { get; set; }
    }
}